package com.bdcom.nms.NmsCollect.toolkit;

import java.awt.AWTException;
import java.awt.Image;
import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.Toolkit;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.text.AbstractDocument.Content;
import javax.swing.text.Document;
import javax.swing.text.PlainDocument;
import javax.swing.text.StringContent;
/**
 * 为程序添加托盘图标
 * @author Administrator
 *
 */
@SuppressWarnings("serial")
public class NmsTray extends JFrame implements ActionListener{
	
	private ConsolePrintStream out;
	
	private JTextArea textarea;
	
	private JScrollPane scrollpane;
	
	private Document doc;
	
	private Content c;
	
	private Image image;
	
	public  NmsTray(){
		
		
		c=new StringContent();
		
		doc=new PlainDocument(c);
		
		textarea=new JTextArea(doc);
		
		JPopupMenu textarea_pop=new JPopupMenu();
		
		JMenuItem textarea_jmu=new JMenuItem("Clean");
		
		textarea_jmu.addActionListener(this);
		
		textarea_jmu.setActionCommand("clean");
		
		textarea_pop.add(textarea_jmu);
		
		textarea.setComponentPopupMenu(textarea_pop);
		
		scrollpane=new JScrollPane(textarea);
		
		this.getContentPane().add(scrollpane);
		
		out=new ConsolePrintStream(textarea);
		
		System.setOut(out);
		
		System.setErr(out);
		
		this.setSize(400, 300);
		
		this.setLocationRelativeTo(null);
		
		image=Toolkit.getDefaultToolkit().createImage(this.getClass().getResource("/image/nmstray.png"));
		
		this.setIconImage(image);
		
		this.setDefaultCloseOperation(HIDE_ON_CLOSE);
		
		setTray();
		
		this.setVisible(true);
		
		
		
	}
	
	
	public void setTray(){
		
		if(!SystemTray.isSupported()){
			return;
		}
		
		SystemTray stray=SystemTray.getSystemTray();
		
		String tooltip = Messages.getLocaleString("nmsCollect");
		
		PopupMenu popup=new PopupMenu();
		
		MenuItem itemopen=new MenuItem("Open");
		
		itemopen.addActionListener(this);
		
		itemopen.setActionCommand("open");
		
		MenuItem itemhide=new MenuItem("Hide");
		
		itemhide.addActionListener(this);
		
		itemhide.setActionCommand("hide");
		
		MenuItem itemexit=new MenuItem("Exit");
		
		itemexit.addActionListener(this);
		
		itemexit.setActionCommand("exit");
		
		popup.add(itemopen);
		
		popup.add(itemhide);
		
		popup.add(itemexit);
		
		
		TrayIcon trayIcon=new TrayIcon(image, tooltip, popup);
		
		
		
		
		
		try {
			
			stray.add(trayIcon);
			
		} catch (AWTException e) {
			// TODO Auto-generated catch block
			System.err.println("Add trayIcon Exception!");
		}
		
	}
	
	

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
		String command=e.getActionCommand();
		
		if("open".equals(command)){
			
			this.setVisible(true);
			
		}else if("hide".equals(command)){
			
			this.setVisible(false);
			
		}else if("exit".equals(command)){
			
			int i=JOptionPane.showConfirmDialog(this,"Exit Program?");
			
			if(i==JOptionPane.YES_OPTION){
				
				System.exit(0);
				
			}
			
		}else if("clean".equals(command)){
			
			textarea.setText("");
			
		}
		
		
		
	}

}
